package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.SmsSendService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.sms.pojo.dto.SmsAvailableCountDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsCancelSendDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsImportExcelResultDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsLogListDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsLogStatisticDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsRabbitMqDTO;
import com.xbongbong.pro.sms.pojo.dto.SmsSendTemplateListDTO;
import com.xbongbong.pro.sms.pojo.vo.SmsCancelSendVO;
import com.xbongbong.pro.sms.pojo.vo.SmsGetAvailableCountVO;
import com.xbongbong.pro.sms.pojo.vo.SmsImportExcelResultVO;
import com.xbongbong.pro.sms.pojo.vo.SmsLogListVO;
import com.xbongbong.pro.sms.pojo.vo.SmsLogStatisticVO;
import com.xbongbong.pro.sms.pojo.vo.SmsRabbitMqVO;
import com.xbongbong.pro.sms.pojo.vo.SmsSendTemplateListVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @description:
 * @Author: hongxiao
 * @date: 2019-01-17 09:45
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/sms")
public class SmsSendController {
    private static final Logger LOG = LoggerFactory.getLogger(SmsSetController.class);

    @Resource
    private SmsSendService smsSendService;

    /**
     * 发送短信初始化
     *
     * @param smsAvailableCountDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:41
     * @modified by:
     * @updateTime: 2019-01-21 13:41
     * @version
     * @since
     */
    @RequestMapping(value = "/getRealCount", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getRealCount(@RequestBody @Valid SmsAvailableCountDTO smsAvailableCountDTO, BindingResult br) {
        XbbResponse<SmsGetAvailableCountVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsGetAvailableCountVO smsGetAvailableCountVO = smsSendService.getAvailableCount(smsAvailableCountDTO);
                response = new XbbResponse<>(smsGetAvailableCountVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    @RequestMapping(value = "/templateList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String templateList(@RequestBody @Valid SmsSendTemplateListDTO smsSendTemplateListDTO, BindingResult br) {
        XbbResponse<SmsSendTemplateListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsSendTemplateListVO smsSendTemplateListVO = smsSendService.templateList(smsSendTemplateListDTO);
                response = new XbbResponse<>(smsSendTemplateListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 发送短信
     *
     * @param smsRabbitMqDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:42
     * @modified by:
     * @updateTime: 2019-01-21 13:42
     * @version
     * @since
     */
    @RequestMapping(value = "/send", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String send(@RequestBody @Valid SmsRabbitMqDTO smsRabbitMqDTO, BindingResult br) {
        XbbResponse<SmsRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                smsSendService.send(smsRabbitMqDTO);
                SmsRabbitMqVO smsRabbitMqVO = new SmsRabbitMqVO();
                response = new XbbResponse<>(smsRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 业务列表（客户/联系人列表）发送短信
     *
     * @param smsRabbitMqDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:43
     * @modified by:
     * @updateTime: 2019-01-21 13:43
     * @version
     * @since
     */
    @RequestMapping(value = "/sendForBusiness", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String sendForBusiness(@RequestBody @Valid SmsRabbitMqDTO smsRabbitMqDTO, BindingResult br) {
        XbbResponse<SmsRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                smsSendService.sendForBusiness(smsRabbitMqDTO);
                SmsRabbitMqVO smsRabbitMqVO = new SmsRabbitMqVO();
                response = new XbbResponse<>(smsRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
//    public String importExcel(@RequestBody @Valid SmsImportExcelDTO smsImportExcelDTO, BindingResult br) {
//        XbbResponse<SmsFeeInfoVO> response;
//        if(br.hasErrors()) {
//            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
//            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
//        } else {
//            try {
//                SmsFeeInfoVO smsFeeInfoVO = smsFeeService.getInfo(smsFeeInfoDTO);
//                response = new XbbResponse<>(smsFeeInfoVO);
//            } catch (XbbException e) {
//                response = new XbbResponse<>(e);
//            }
//        }
//        return JSON.toJSONString(response);
//    }

    /**
     * 获取导入短信接收人结果
     *
     * @param smsImportExcelResultDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:44
     * @modified by:
     * @updateTime: 2019-01-21 13:44
     * @version
     * @since
     */
    @RequestMapping(value = "/importExcel/result", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String importExcelResult(@RequestBody @Valid SmsImportExcelResultDTO smsImportExcelResultDTO, BindingResult br) {
        XbbResponse<SmsImportExcelResultVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsImportExcelResultVO smsImportExcelResultVO = smsSendService.importExcelResult(smsImportExcelResultDTO);
                response = new XbbResponse<>(smsImportExcelResultVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 取消定时发送
     *
     * @param smsCancelSendDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:45
     * @modified by:
     * @updateTime: 2019-01-21 13:45
     * @version
     * @since
     */
    @RequestMapping(value = "/cancelSend", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cancelSend(@RequestBody @Valid SmsCancelSendDTO smsCancelSendDTO, BindingResult br) {
        XbbResponse<SmsCancelSendVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsCancelSendVO smsCancelSendVO = smsSendService.cancelSend(smsCancelSendDTO);
                response = new XbbResponse<>(smsCancelSendVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 短信重发
     *
     * @param smsRabbitMqDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:45
     * @modified by:
     * @updateTime: 2019-01-21 13:45
     * @version
     * @since
     */
    @RequestMapping(value = "/reSend", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String reSend(@RequestBody @Valid SmsRabbitMqDTO smsRabbitMqDTO, BindingResult br) {
        XbbResponse<SmsRabbitMqVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                smsSendService.resend(smsRabbitMqDTO);
                SmsRabbitMqVO smsRabbitMqVO = new SmsRabbitMqVO();
                response = new XbbResponse<>(smsRabbitMqVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 短信记录列表
     *
     * @param smsLogListDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:46
     * @modified by:
     * @updateTime: 2019-01-21 13:46
     * @version
     * @since
     */
    @RequestMapping(value = "/log/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String logList(@RequestBody @Valid SmsLogListDTO smsLogListDTO, BindingResult br) {
        XbbResponse<SmsLogListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsLogListVO smsLogListVO = smsSendService.logList(smsLogListDTO);
                response = new XbbResponse<>(smsLogListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 短信记录统计
     *
     * @param smsLogStatisticDTO 入参
     * @param br
     * @return
     * @author hongxiao
     * @addTime: 2019-01-21 13:46
     * @modified by:
     * @updateTime: 2019-01-21 13:46
     * @version
     * @since
     */
    @RequestMapping(value = "/log/getStatistic", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String logStatistic(@RequestBody @Valid SmsLogStatisticDTO smsLogStatisticDTO, BindingResult br) {
        XbbResponse<SmsLogStatisticVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                SmsLogStatisticVO smsLogStatisticVO = smsSendService.logStatistic(smsLogStatisticDTO);
                response = new XbbResponse<>(smsLogStatisticVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
